package cn.iocoder.yudao.module.resource.controller.admin.keyword;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.resource.controller.admin.keyword.vo.KeyWordPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.keyword.vo.KeyWordRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.keyword.vo.KeyWordSaveReqVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.keyword.KeyWordDO;
import cn.iocoder.yudao.module.resource.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.resource.service.audiomark.AudioMarkService;
import cn.iocoder.yudao.module.resource.service.keyword.KeyWordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 关键字")
@RestController
@RequestMapping("/lm/key-word")
@Validated
public class KeyWordController {

    @Resource
    private KeyWordService keyWordService;
    @Resource
    private AudioMarkService audioMarkService;


    @PostMapping("/create")
    @Operation(summary = "创建关键字")
    @PreAuthorize("@ss.hasPermission('lm:key-word:create')")
    public CommonResult<Integer> createKeyWord(@Valid @RequestBody KeyWordSaveReqVO createReqVO) {
        return success(keyWordService.createKeyWord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新关键字")
    @PreAuthorize("@ss.hasPermission('lm:key-word:update')")
    public CommonResult<Boolean> updateKeyWord(@Valid @RequestBody KeyWordSaveReqVO updateReqVO) {
        keyWordService.updateKeyWord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除关键字")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lm:key-word:delete')")
    public CommonResult<Boolean> deleteKeyWord(@RequestParam("id") Integer id) {
        // 如果有标记的音频，不能删除
        if (this.audioMarkService.isHaveMarkAudioWithKeyword(id)) {
            throw new ServiceException(ErrorCodeConstants.KEY_WORD_HAD_MARK_AUDIO);
        }
        keyWordService.deleteKeyWord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得关键字")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lm:key-word:query')")
    public CommonResult<KeyWordRespVO> getKeyWord(@RequestParam("id") Integer id) {
        KeyWordDO keyWord = keyWordService.getKeyWord(id);
        return success(BeanUtils.toBean(keyWord, KeyWordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得关键字分页")
    @PreAuthorize("@ss.hasPermission('lm:key-word:query')")
    public CommonResult<PageResult<KeyWordRespVO>> getKeyWordPage(@Valid KeyWordPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<KeyWordDO> pageResult = keyWordService.getKeyWordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyWordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出关键字 Excel")
    @PreAuthorize("@ss.hasPermission('lm:key-word:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKeyWordExcel(@Valid KeyWordPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyWordDO> list = keyWordService.getKeyWordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "关键字.xls", "数据", KeyWordRespVO.class,
                BeanUtils.toBean(list, KeyWordRespVO.class));
    }

}